from django.shortcuts import render

from django.contrib.auth.hashers import make_password,check_password
from django.http import JsonResponse,HttpResponse
from ..models import *
from ..utils import jwtUtils
from ..utils.func import filterImageUrl, getCurrentDate
from ..utils.jsonSerialize import serialize_models
import time

from ..utils.fileSystem import uploadFileAssets


# 测试接口
def test(request):
    return HttpResponse("成功访问django接口")

# 登录帐号
def login(request):
    data = request.POST.dict()
    data.pop("csrfmiddlewaretoken",None)
    # 先检测学生表
    user_obj = UserMsg.objects.filter(phone=data['phone'])
    user_obj_filter = user_obj.first()
    if not user_obj_filter:
        return JsonResponse ({'msg': '登录失败！帐号不存在！', 'code': 403})
    else:
        if user_obj_filter.is_delete == '1':
          return JsonResponse ({'msg': '失败！当前账号已经注销！', 'code': 403})
        elif user_obj_filter.type == '0':
          return JsonResponse ({'msg': '访问失败！当前账号已被禁用，请等待解封！', 'code': 403})

        # 检测密码是否正确
        res = check_password (data['password'], user_obj_filter.password)
        if not res:
            return JsonResponse ({'msg': '登录失败！密码错误！', 'code': 403})
        else:
            # 生成token身份认证, 并存储在数据库
            token = jwtUtils.make_jwt(user_obj_filter.id)
            user_obj.update(token=token)
            # 序列化数据集对象
            user_obj_data = serialize_models(user_obj_filter)
            filterImageUrl(request,user_obj_data,'avatar_url')
            del user_obj_data['password']
            del user_obj_data['token']
            # 获取优惠券汇总
            # 获取当前时间
            currentDate = getCurrentDate()
            request_Data = {}
            request_Data["coupon_user_id"] = user_obj_filter.id
            request_Data["start_time__lte"] = currentDate
            request_Data["end_time__gte"] = currentDate
            request_Data["status"] = '1'
            count = Coupons.objects.filter(**request_Data).count()
            user_obj_data['coupon'] = count
            return JsonResponse ({'msg': '登录成功！','token':token, 'userinfo':user_obj_data ,'code': 200})

# 注册帐号
def register(request):
    data = request.POST.dict()
    # 添加注册时间
    data['register_time'] = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
    data['is_delete'] = '0'
    data['type'] = '1'
    # 密码加密
    data['password'] = make_password (data['password'], None, 'pbkdf2_sha256')

    # 查询是否帐号已经被注册
    obj = UserMsg.objects.filter(phone=data['phone']).first()
    if not obj:
        # 如果有头像上传，则保存头像。
        filename = uploadFileAssets(request,'avatar')
        data["avatar_url"] = filename
        UserMsg(**data).save()
        return JsonResponse ({'msg': '注册成功！', 'code': 200})
    else:
        return JsonResponse ({'msg': '当前手机号已经被注册，请重新输入！', 'code': 403})

## 退出登录
def logout(request):
    # 获取请求头上的token,转换成user_id
    user_id = jwtUtils.get_parse_token(request)
    user_obj = UserMsg.objects.filter(id=user_id)
    user_obj.update(token="")
    return JsonResponse ({'msg': '退出登录成功！', 'code': 200})

## 注销帐号
def unregister(request):
    # 获取请求头上的token,转换成user_id
    user_id = jwtUtils.get_parse_token(request)
    user_obj = UserMsg.objects.filter(id=user_id)
    # 添加注册时间
    data = {
        'token':'',
        'delete_time':time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),
        'is_delete':'1'
    }
    user_obj.update(**data)
    return JsonResponse ({'msg': '注销帐号成功！', 'code': 200})